/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dtn.wsn.dao;

import com.dtn.wsn.database.ConnectionPool;

import com.dtn.wsn.entity.Data;
import java.sql.SQLException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Data Access object for Data entity
 * @author hao
 */
public class DataDAO {

    private ConnectionPool connectionPool;

    public DataDAO(ConnectionPool connectionPool) {
        this.connectionPool = connectionPool;
    }

    /**
     * Get Data object by spotname 
     * @param spotname
     * @return 
     */
    public Data queryDataBySpotname(String spotname) {
        Data data = new Data();
        String sql = "select * from data,sunspot where data.spot_id= sunspot.spot_id and spotname = '" + spotname + "'";
        try {
            Connection cn = connectionPool.getConnection();
            Statement stmt = cn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                //data.setrs.getString("spotId");
                data.setSensingId(Integer.parseInt(rs.getString("sensing_id")));
                data.setSpotId(Integer.parseInt(rs.getString("spot_id")));
                data.setTemperature(Double.parseDouble(rs.getString("temperature")));
            }
            connectionPool.freeConnection(cn);
        } catch (SQLException e) {
            System.out.println("insert student record error :" + e);
        }

        return data;
    }
    
    /**
     * insert one data into database
     * @param data
     * @return 
     */
    public int InsertData(Data data) {

        int updateQuery = 0;
        String sql = "INSERT INTO data(spot_id,timestamp,light,temperature,battery) values"
                + "('" + data.getSpotId() + "','" + data.getTimestample() + "','" + data.getLightLevel()
                + "','" + data.getTemperature() + "','" + data.getBattery() + "')";
        try {
            Connection cn = connectionPool.getConnection();
            Statement stmt = cn.createStatement();
            updateQuery = stmt.executeUpdate(sql);

            connectionPool.freeConnection(cn);
        } catch (SQLException ex) {
            Logger.getLogger(DataDAO.class.getName()).log(Level.SEVERE, null, ex);
        }


        return updateQuery;
    }
    
    /**
     * insert a list of data into database
     * @param dataList
     * @return 
     */

    public int InsertData(ArrayList<Data> dataList) {
        int updateQuery = 0;
        
        try {
            Connection cn = connectionPool.getConnection();
            for(int i = 0; i< dataList.size();i++){
                Data data = dataList.get(i);
                String sql = "INSERT INTO data(spot_id,timestamp,light,temperature,battery) values"
                + "('" + data.getSpotId() + "','" + data.getTimestample() + "','" + data.getLightLevel()
                + "','" + data.getTemperature() + "','" + data.getBattery() + "')";
                Statement stmt = cn.createStatement();
                updateQuery = stmt.executeUpdate(sql);
                if(updateQuery == 0)
                {
                    System.out.println("Erro");
                    break;
                }
            }
           
            connectionPool.freeConnection(cn);
        } catch (SQLException ex) {
            Logger.getLogger(DataDAO.class.getName()).log(Level.SEVERE, null, ex);
        }


        return updateQuery;
    }
}
